package com.eastfair.pay.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.annotation.annotation.base.BaseControllerInfo;
import com.eastfair.annotation.annotation.log.SysLog;
import com.eastfair.annotation.annotation.security.PreAuth;
import com.eastfair.boot.controller.SuperController;
import com.eastfair.boot.request.PageParams;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.echo.core.EchoService;
import com.eastfair.pay.demand.SettlementGroup;
import com.eastfair.pay.dto.InvoiceCallbackDTO;
import com.eastfair.pay.dto.SettlementPageQuery;
import com.eastfair.pay.dto.SettlementSaveDTO;
import com.eastfair.pay.dto.SettlementUpdateDTO;
import com.eastfair.pay.entity.Settlement;
import com.eastfair.pay.service.SettlementService;
import com.eastfair.pay.vo.SettlementVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 前端控制器
 * 结算单
 * </p>
 *
 * @author clm
 * @date 2022-06-08
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/settlement")
@Api(value = "Settlement", tags = "结算单")
@PreAuth(replace = "pay:settlement:")
public class SettlementController extends SuperController<SettlementService, Long, Settlement, SettlementPageQuery, SettlementSaveDTO, SettlementUpdateDTO> {

    @Autowired
    private EchoService echoService;

    @Override
    public void handlerResult(IPage<Settlement> page) {
        // 想让返回值实现自动回显，请将此行代码打开
        // echoService.action(page);
    }
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<Settlement> settlementList = list.stream().map((map) -> {
            Settlement settlement = Settlement.builder().build();
            //TODO 请在这里完成转换
            return settlement;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(settlementList));
    }

    /**
     * 处理参数
     *
     * @param params 分页参数
     */
    @Override
    public void handlerQueryParams(PageParams<SettlementPageQuery> params) {
        SettlementPageQuery pageQuery = params.getModel();
        //分页处理
        if(ContextUtil.getProjectId()!=null){
            pageQuery.setProjectId(ContextUtil.getProjectId());
        }
    }

    /**
     * 结算保存
     * @param saveDTO
     * @return
     */
    @PostMapping("/settlementSave")
    @SysLog("结算保存")
    public  R settlementSave(@RequestBody @Validated SettlementSaveDTO saveDTO){
        //结算保存
         baseService.settlementSaveDeail(saveDTO);

        return R.success();
    }


    @PostMapping("/updateRefundApplyStatus")
    public  R updateRefundApplyStatus(@RequestBody @Validated Settlement settlement){
        //结算退款更新
        baseService.updateById(settlement);

        return R.success();
    }



    /**
     * 结算单查询
     * @return
     */
    @PostMapping("/queryList")
    @SysLog("结算单查询")
    public R<Page<SettlementVO>> queryList(@RequestBody @Validated PageParams<SettlementPageQuery> params){

        return R.success(baseService.queryPageList(params));
    }

    /**
     * 结算详情
     * @param settlementId
     * @return
     */
    @GetMapping("/settlementQueryById")
    @BaseControllerInfo(voGroup = {SettlementGroup.settlementQueryById.class})
    public R<SettlementVO>  settlementQueryById(@RequestParam("settlementId") @Validated @NotNull(message = "结算单ID不能为为空") Long settlementId){
        log.info("结算详情查询 入参 settlementId {} ",settlementId);

        return  R.success(baseService.settlementQueryById(settlementId));
    }

    /**
     * 开票回填
     *
     * @param invoiceCallbackDTO
     * @return the r
     */
    @PostMapping("/update/invoiceAmount")
    public R<Boolean> updateForInvoice(@RequestBody @Validated InvoiceCallbackDTO invoiceCallbackDTO) {
        log.info("updateForInvoice - 开票回填, invoiceCallbackDTO={}", invoiceCallbackDTO);
        baseService.updateForInvoice(invoiceCallbackDTO);
        return R.success();
    }
}
